//****************************************************************
//* Copyright (c) 2004 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   EconomicSourceForm.java  $$
//* $$Revision:   1.1  $$
//* $$Author:   JLOPE262  $$
//* $$Date:   Nov 21 2008 14:27:08  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.view.form;

import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese02Econsource;

/**
 * 
 * @author fcolucc1
 * 
 */
public class EconomicSourceForm extends ValidatorForm {
    private String section;
    private String code;
    private String name;
    private String codeFind;
    private String nameFind;
    private Collection economicSources;
    private Teese02Econsource economicSourceSelected;

    /**
     * Resetea el formulario
     * 
     */
    public void resetForm() {
        this.section = "";
        this.code = null;
        this.name = null;
        this.codeFind = null;
        this.nameFind = null;
        this.economicSources = null;
        this.economicSourceSelected = null;
    }

    /**
     * Limpia los campos utilizados en el form para la creacion o actualizacion
     * de Economic Sources
     * 
     */
    public void clearCreateUpdateFields() {
        this.code = null;
        this.name = null;
    }

    public void cleanFindFields() {
        this.codeFind = null;
        this.nameFind = null;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCodeFind() {
        return codeFind;
    }

    public void setCodeFind(String codeFind) {
        this.codeFind = codeFind;
    }

    public Collection getEconomicSources() {
        return economicSources;
    }

    public void setEconomicSources(Collection economicSources) {
        this.economicSources = economicSources;
    }

    public String getNameFind() {
        return nameFind;
    }

    public void setNameFind(String nameFind) {
        this.nameFind = nameFind;
    }

    /**
     * Setea el Economic Source seleccionado para luego ser modificado
     * 
     * @param key
     * @param value
     */
    public void setEditMapped(String key, String value) {
        List economicSources = (List) this.getEconomicSources();

        this.setEconomicSourceSelected((Teese02Econsource) economicSources
                .get(Integer.parseInt(key) - 1));
    }

    /**
     * Setea el Economic Source seleccionada para luego ser eliminado
     * 
     * @param key
     * @param value
     */
    public void setDeleteMapped(String key, String value) {
        setEditMapped(key, value);
    }

    public Teese02Econsource getEconomicSourceSelected() {
        return economicSourceSelected;
    }

    public void setEconomicSourceSelected(
                                          Teese02Econsource economicSourceSelected) {
        this.economicSourceSelected = economicSourceSelected;
    }

    /**
     * Setea los campos del cuadro de creacion/modificacion de Economic Sources
     * 
     * @param economicSource
     */
    public void setEconomicSourceForEdit(Teese02Econsource economicSource) {
        this.setCode(String
                .valueOf(economicSource.getEese02EconsourceNumberK()));
        this.setName(economicSource.getEese02EconsourceNameN());
    }

    /**
     * Setea los campos del Economic Source para su posterior creacion
     * 
     * @return Economic Source con los campos minimos seteados para la posterior
     * creacion
     */
    public Teese02Econsource getEconomicSourceForCreate() {
        Teese02Econsource economicSource = new Teese02Econsource();

        economicSource.setEese02EconsourceNameN(this.getName());
        return economicSource;
    }

    /**
     * Setea los campos del Economic Source para su posterior actualizacion
     * 
     * @param economicSource
     * Economic Source a actualizar
     * @return Economic Source con los campos a actualizar modificados
     */
    public Teese02Econsource getEconomicSourceForUpdate(
                                                        Teese02Econsource economicSource) {
        economicSource.setEese02EconsourceNameN(this.getName());

        return economicSource;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {
        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);

        if ("find".equalsIgnoreCase(this.getSection())) {
            if (StringUtils.isNotBlank(this.getCodeFind())) {
                if (this.getCodeFind().matches("[0-9]+")) {
                    try {
                        if (Integer.parseInt(this.getCodeFind()) == 0) {
                            ae
                                    .add(
                                            ActionMessages.GLOBAL_MESSAGE,
                                            new ActionMessage(
                                                    "errors.integergtzero",
                                                    resources
                                                            .getMessage("economicSource.label.code")));
                        }
                    } catch (NumberFormatException e) {
                        ae
                                .add(
                                        ActionMessages.GLOBAL_MESSAGE,
                                        new ActionMessage(
                                                "errors.invalid",
                                                resources
                                                        .getMessage("economicSource.label.code")));
                    }
                } else {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.integergtzero", resources
                                    .getMessage("economicSource.label.code")));
                }
            }
        } else if ("createUpdate".equalsIgnoreCase(this.getSection())) {
            if (StringUtils.isBlank(this.getName())) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "errors.required", resources
                                .getMessage("economicSource.label.name")));
            }
        }

        return ae;
    }

}
